# RUN: llc -start-after=phi-node-elimination -stop-after=virtregrewriter %s -mtriple=x86_64-unknown-unknown -o - -experimental-debug-variable-locations=false | FileCheck %s
#
# Test that when a livedebugvars interval crosses a basic block boundary,
# DBG_VALUEs are created in each covered basic block. In the IR below, control
# flow moves from the bottom upwards, and the DBG_VALUEs from the entry block
# to bb.1 become a single range. Check that they're transformed back into two
# DBG_VALUEs after regalloc.

# These checks look at the entry and bb.1 blocks, and ensure that there is a
# DBG_VALUE in each.

# CHECK:       ![[VARNUM:[0-9]+]] = !DILocalVariable(name: "b",
# CHECK-LABEL: name: bar
# CHECK-LABEL: bb.0.entry:
# CHECK:       DBG_VALUE $[[SOMEREG:[a-z]+]], $noreg, ![[VARNUM]], !DIExpression()
# CHECK-NEXT:  JMP_1 %bb.3
# CHECK-LABEL: bb.1.bb1:
# CHECK:       DBG_VALUE $[[SOMEREG]], $noreg, ![[VARNUM]], !DIExpression()
# CHECK-NEXT:  JMP_1 %bb.4

--- |
  ; ModuleID = 'tmp.ll'
  source_filename = "tmp.ll"
  target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
  
  %struct.a = type { i32 }
  
  ; Function Attrs: nounwind ssp
  define i32 @bar(%struct.a* nocapture %b, i32 %shoes) !dbg !4 {
  entry:
    tail call void @llvm.dbg.value(metadata i32 %shoes, metadata !9, metadata !DIExpression()), !dbg !16
    %tmp1 = getelementptr inbounds %struct.a, %struct.a* %b, i64 0, i32 0, !dbg !17
    br label %bb3
  
  bb1:                                              ; preds = %bb2
    tail call void @llvm.dbg.value(metadata i32 %shoes, metadata !9, metadata !DIExpression()), !dbg !16
    %add = add nsw i32 %tmp2, 1, !dbg !18
    br label %exit
  
  bb2:                                              ; preds = %bb3
    tail call void @llvm.dbg.value(metadata i32 %tmp2, metadata !14, metadata !DIExpression()), !dbg !17
    %call = tail call i32 (...) @foo(i32 %tmp2), !dbg !19
    br label %bb1
  
  bb3:                                              ; preds = %entry
    %tmp2 = load i32, i32* %tmp1, align 4, !dbg !17
    br label %bb2
  
  exit:                                             ; preds = %bb1
    ret i32 %shoes, !dbg !18
  }
  
  declare i32 @foo(...)
  
  ; Function Attrs: nounwind readnone speculatable willreturn
  declare void @llvm.dbg.value(metadata, metadata, metadata)
  
  ; Function Attrs: nounwind
  declare void @llvm.stackprotector(i8*, i8**)
  
  !llvm.dbg.cu = !{!0}
  !llvm.module.flags = !{!3}
  
  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 2.9 (trunk 122997)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !2)
  !1 = !DIFile(filename: "bar.c", directory: "/private/tmp")
  !2 = !{}
  !3 = !{i32 1, !"Debug Info Version", i32 3}
  !4 = distinct !DISubprogram(name: "bar", scope: !1, file: !1, line: 5, type: !5, virtualIndex: 6, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
  !5 = !DISubroutineType(types: !6)
  !6 = !{!7}
  !7 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
  !8 = !{!9, !14}
  !9 = !DILocalVariable(name: "b", arg: 1, scope: !4, file: !1, line: 5, type: !10)
  !10 = !DIDerivedType(tag: DW_TAG_pointer_type, scope: !0, baseType: !11, size: 64, align: 64)
  !11 = !DICompositeType(tag: DW_TAG_structure_type, name: "a", scope: !0, file: !1, line: 1, size: 32, align: 32, elements: !12)
  !12 = !{!13}
  !13 = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: !1, file: !1, line: 2, baseType: !7, size: 32, align: 32)
  !14 = !DILocalVariable(name: "x", scope: !15, file: !1, line: 6, type: !7)
  !15 = distinct !DILexicalBlock(scope: !4, file: !1, line: 5, column: 22)
  !16 = !DILocation(line: 5, column: 19, scope: !4)
  !17 = !DILocation(line: 6, column: 14, scope: !15)
  !18 = !DILocation(line: 8, column: 2, scope: !15)
  !19 = !DILocation(line: 7, column: 2, scope: !15)

...
---
name:            bar
alignment:       16
tracksRegLiveness: true
registers:
  - { id: 0, class: gr64 }
  - { id: 1, class: gr32 }
  - { id: 2, class: gr64 }
  - { id: 3, class: gr64 }
  - { id: 4, class: gr32 }
  - { id: 5, class: gr32 }
  - { id: 6, class: gr32 }
  - { id: 7, class: gr32 }
liveins:
  - { reg: '$rdi', virtual-reg: '%2' }
  - { reg: '$esi', virtual-reg: '%4' }
frameInfo:
  hasCalls:        true
machineFunctionInfo: {}
body:             |
  bb.0.entry:
    liveins: $rdi, $esi
  
    %4:gr32 = COPY $esi
    %2:gr64 = COPY $rdi
    %3:gr64 = COPY killed %2
    %5:gr32 = COPY killed %4
    DBG_VALUE %5, $noreg, !9, !DIExpression(), debug-location !16
    JMP_1 %bb.3
  
  bb.1.bb1:
    DBG_VALUE %5, $noreg, !9, !DIExpression(), debug-location !16
    JMP_1 %bb.4
  
  bb.2.bb2:
    DBG_VALUE %6, $noreg, !14, !DIExpression(), debug-location !17
    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !19
    $edi = COPY %6, debug-location !19
    $al = MOV8ri 0, debug-location !19
    CALL64pcrel32 @foo, csr_64, implicit $rsp, implicit $ssp, implicit $al, implicit $edi, implicit-def $eax, debug-location !19
    ADJCALLSTACKUP64 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !19
    %7:gr32 = COPY $eax, debug-location !19
    JMP_1 %bb.1
  
  bb.3.bb3:
    %6:gr32 = MOV32rm %3, 1, $noreg, 0, $noreg, debug-location !17 :: (load (s32) from %ir.tmp1)
    JMP_1 %bb.2
  
  bb.4.exit:
    $eax = COPY %5, debug-location !18
    RET64 implicit $eax, debug-location !18

...
